package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.form.MdmsUser;
import com.fengwei.requestdata.dataChangeApply.MdmsDatachangeApplyAddData;
import com.fengwei.requestdata.dataChangeApply.MdmsDatachangeApplyCheckData;
import com.fengwei.requestdata.dataChangeApply.MdmsDatachangeApplyDelData;
import com.fengwei.requestdata.dataChangeApply.MdmsDatachangeApplyEditData;
import com.fengwei.requestdata.dataChangeApply.MdmsDatachangeApplyPagingListData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.MdmsDatachangeApplyService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Classname MdmsDatachangeApplyController
 * @Description 数据变更申请Controller
 * @Date 2020/11/25 10:47
 * @Author ludexin
 */
@Controller
public class MdmsDatachangeApplyController {
    private final static Logger logger = LoggerFactory.getLogger(MdmsDatachangeApplyController.class);

    @Autowired
    private MdmsDatachangeApplyService mdmsDatachangeApplyService;

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @GetMapping(value = "/mdmsDatachangeApply/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        model.addAttribute("userType", mdmsUser.getUserType());
        return "mdmsDatachangeApply";
    }

    /**
     * 数据变更申请查询
     */
    @PostMapping(path = "/mdmsDatachangeApply/pagingList")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA','USER')")
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsDatachangeApplyPagingListData mdmsDatachangeApplyPagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsDatachangeApplyPagingListData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatachangeApplyService.pagingList(mdmsDatachangeApplyPagingListData, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 添加申请
     */
    @PostMapping(path = "/mdmsDatachangeApply/add")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA','USER')")
    public Map<String, Object> add(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsDatachangeApplyAddData mdmsDatachangeApplyAddData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatachangeApplyAddData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatachangeApplyService.add(mdmsDatachangeApplyAddData, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 修改申请
     */
    @PostMapping(path = "/mdmsDatachangeApply/edit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA','USER')")
    public Map<String, Object> edit(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatachangeApplyEditData mdmsDatachangeApplyEditData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatachangeApplyEditData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatachangeApplyService.edit(mdmsDatachangeApplyEditData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 删除申请
     */
    @PostMapping(path = "/mdmsDatachangeApply/del")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA','USER')")
    public Map<String, Object> del(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatachangeApplyDelData mdmsDatachangeApplyDelData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatachangeApplyDelData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatachangeApplyService.del(mdmsDatachangeApplyDelData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 申请审核
     */
    @PostMapping(path = "/mdmsDatachangeApply/check")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA')")
    public Map<String, Object> check(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsDatachangeApplyCheckData mdmsDatachangeApplyCheckData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatachangeApplyCheckData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatachangeApplyService.check(mdmsDatachangeApplyCheckData, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

}
